/**
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

{% macro generate_complete_test(cases, test_type='positive', declaration_type='function') %}
{% for c in cases %}
{% for type_name in [c.type, c.alias] | select %}

/*---
desc: The return type of an async function must be Promise<{{type_name}}>.
---*/

{% if test_type == 'negative' %}
// Promise reject
async function returnsReject(): Promise<{{type_name}}> {
    return Promise.reject(new Error("Operation failed"));
}
{% elif test_type == 'positive' %}
{% if type_name in ['Byte', 'Short'] %}
const value: {{type_name}} = new {{type_name}}({{c.value}})
{% else %}
const value: {{type_name}} = {{c.value}}
{% endif %}
// Promise resolve
async function returnsPromise(): Promise<{{type_name}}> {
    return value;
}

function returnsValue(): {{type_name}} {
    return value;
}
{% endif %}

async function startTest(){
    {% if test_type == 'negative' %}
    try {
        await returnsReject();
        arktest.assertTrue(false, "Should have thrown");
    } catch (e) {
        let err = e as Error;
        arktest.assertEQ(err.message, "Operation failed");
    }

    {% elif test_type == 'positive' %}
    // Check async func returns Promise
    const promiseResult = returnsPromise();
    arktest.assertTrue(promiseResult instanceof Promise, "Async function should return Promise");

    // Check it possible to use await with Promise
    const awaitedResult = await promiseResult;
    arktest.assertTrue(awaitedResult instanceof {{type_name}})
    const value = {{c.value}};
    arktest.assertEQ(awaitedResult, value, `Awaited result should be ${value}`);

    // Check simple func don't returns Promise
    const funcResult = returnsValue();
    arktest.assertTrue(!(funcResult instanceof Promise), "Sync function should not return Promise");
    {% endif %}
}

function main() {
    waitForCompletion(startTest); 
}
{% endfor %}
{% endfor %}
{% endmacro %}